home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3074 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  65 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: [Q] Best way to "define" flag values?
  5. Message-ID: <marnoldDLK19q.4EM@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <marnoldDLIv9w.1s4@netcom.com> <4du2ug$nqt@ixnews8.ix.netcom.com>
  8. Date: Sun, 21 Jan 1996 23:29:49 GMT
  9. Sender: marnold@netcom23.netcom.com
  10.  
  11. wkdugan@ix.netcom.com (Bill Dugan) writes:
  12.  
  13. >marnold@netcom.com (Matt Arnold) wrote:
  14.  
  15. >snip
  16.  
  17. >>However, I have never quite found a good substitute for declaring
  18. >>values for flags.  Passing flags to a function, for instance, is
  19.                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. >>a very common construct, and it's typically arranged like so...
  21.  
  22. >>   #define FLAG_A 0x0001
  23. >>   #define FLAG_B 0x0010
  24. >>   #define FLAG_C 0x0100
  25.  
  26. >snip
  27.  
  28. >Look up bit fields in your C or C++ reference.
  29.  
  30. Duh, I know what bit fields are, thank you very much.  But, they are not
  31. very useful for passing flags to a function, which was the main gist of 
  32. my posting (which was snipped).
  33.  
  34.    FLAGS flags;   // assume this is a bit field
  35.  
  36.    flags.a = TRUE;
  37.    flags.b = FALSE;
  38.    flags.c = TRUE;
  39.    FuncTakingFlags(flags);
  40.  
  41. ...is NOT as convenient as the traditional ...
  42.  
  43.    FuncTakingFlags(FLAG_A | FLAG_C);
  44.  
  45. The bit field apporach is far too verbose, in my opinion, and can make 
  46. certain coding styles difficult to use (ie, you always have to declare a
  47. bit field to pass to functions, etc.).  And, it's not as easy to combine 
  48. multiple bit fields into a single result (as you can with integer flag
  49. values, using the bitwise OR operator, for instance).
  50.  
  51. Can you show us an example of how using bit fields for passing flags to 
  52. functions has some kind of clear advantage over #define'd bit values?
  53.  
  54. Anyone have any better ideas?  
  55.  
  56. Regards,
  57. -------------------------------------------------------------------------
  58. Matt Arnold                       |        | ||| | |||| |  | | || ||
  59. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  60. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  61. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  62. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  63. -------------------------------------------------------------------------
  64.  
  65.